package com.example.demo;

import java.awt.*;
import java.util.ArrayList;

public abstract class Minion extends GameObject{

    //是否生成下一个小兵
    private boolean nextMinion = true;
    //是否生成下一波小兵
    private boolean nextLine = true;
    //生成小兵数量
    private int minionCount = 0;
    //是否检测到目标
    private boolean ifFindTarget = false;

    public Minion(GameFrame gameFrame){
        super(gameFrame);
        setHp(1000);
        setCurrentHp(getHp());
        setDis(100);
        setAttackCoolDownTime(2000);
    }

    public abstract void move(ArrayList<GameObject> objList);

    public void findTarget(ArrayList<GameObject> objList){
        for (GameObject obj : objList){
            if (revIntersectsCir(obj.getRec(),getX(),getY(),200) && isAlive()){
                setTarget(obj);
                setIfFindTarget(true);
            }
        }
        if (objList == gameFrame.blueList){
            if (revIntersectsCir(gameFrame.player.getRec(),getX(),getY(),200) && isAlive()){
                setTarget(gameFrame.player);
                setIfFindTarget(true);
            }
        }
    }

    public void moveToTarget(){
        int dis = (int) getDis(getX(),getY(),getTarget().getX(),getTarget().getY());
        if(dis>0) {
            int xSpeed = (int) (getSpd() * (getTarget().getX() - getX()) / dis);
            int ySpeed = (int) (getSpd() * (getTarget().getY() - getY()) / dis);
            setX(getX() + xSpeed);
            setY(getY() + ySpeed);
        }else if (dis<=0){
            gameFrame.removeList.add(this);
        }
    }

    public void createMinion(GameFrame gameFrame, ArrayList<GameObject> minionList){
        if (nextLine){
            if (nextMinion){
                //蓝色小兵
                if (minionList == this.gameFrame.blueList){
                    MinionBlue mb = new MinionBlue(gameFrame);
                    gameFrame.objList.add(mb);
                    minionList.add(mb);
                }
                //红色小兵
                else {
                    MinionRed mr = new MinionRed(gameFrame);
                    gameFrame.objList.add(mr);
                    minionList.add(mr);
                }
                minionCount++;
                new NextMinion().start();
            }
            if (minionCount == 3){
                minionCount = 0;
                new NextLine().start();
            }
        }
    }

    //每个小兵生成的时间
    class NextMinion extends Thread{
        public void run(){
            nextMinion = false;
            //休眠1.5s
            try{
                Thread.sleep(1500);
            }catch (Exception e){
                e.printStackTrace();
            }
            nextMinion = true;
            //线程终止
            this.stop();
        }
    }

    //每波小兵生成的时间
    class NextLine extends Thread{
        public void run(){
            nextLine = false;
            //休眠15s
            try{
                Thread.sleep(15000);
            }catch (Exception e){
                e.printStackTrace();
            }
            nextLine = true;
            //线程终止
            this.stop();
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        //生命值为0
        if (getCurrentHp() <= 0){
            setAlive(false);
            gameFrame.removeList.add(this);
            if (this instanceof MinionBlue){
                gameFrame.blueList.remove(this);
            }else if (this instanceof MinionRed){
                gameFrame.redList.remove(this);
            }
        }else {
            //添加生命值
            if (this instanceof MinionBlue) {
                this.addHp(g, 17, 28, 45, 10, Color.GREEN);
                move(gameFrame.redList);
            } else {
                this.addHp(g, 17, 28, 45, 10, Color.RED);
                move(gameFrame.blueList);
            }
            g.drawImage(getImg(), getX() - 16, getY() - 16, null);
            g.setColor(Color.RED);
            g.fillOval(getX(), getY(), 10, 10);
            g.drawRect(getX() - 16, getY() - 16, 45, 45);
            g.drawOval(getX()-200,getY()-200,400,400);
            //小兵移动
            if (this instanceof MinionBlue){
                move(gameFrame.redList);
            }else {
                move(gameFrame.blueList);
            }
        }
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(getX()-16,getY()-16,45,45);
    }

    public boolean isIfFindTarget() {
        return ifFindTarget;
    }

    public void setIfFindTarget(boolean ifFindTarget) {
        this.ifFindTarget = ifFindTarget;
    }

}
